package com.vastcom.sigma.admin.action;

import com.google.gson.Gson;
import com.netflix.discovery.converters.Auto;
import com.sun.org.apache.regexp.internal.RE;
import com.sun.xml.bind.v2.TODO;
import com.vastcom.sigma.admin.model.*;
import com.vastcom.sigma.admin.service.AuthAdminService;
import com.vastcom.sigma.admin.service.BlockAdminService;
import com.vastcom.sigma.admin.service.ManageService;
import com.vastcom.sigma.admin.to.BlockChain;
import com.vastcom.sigma.admin.tools.BaseImgTools;
import com.vastcom.sigma.admin.tools.CanUpload;
import com.vastcom.sigma.admin.tools.Tools;
import com.vastcom.sigma.admin.vo.NzSelOption;
import com.vastcom.sigma.admin.vo.NzTreeVo;
import com.vastcom.sigma.admin.vo.PageParm;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/member")
public class MemberAction {

    @Autowired
    private ManageService manageService;

    @Value("${spring.uploadpath}")
    private String filepath;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private AuthAdminService authAdminService;

    @Autowired
    private BlockAdminService blockAdminService;

    @PostMapping("saveGys")
    @ResponseBody
    public HashMap saveGys(@RequestBody Gys gys) {
        HashMap hashMap = new HashMap();
        try {
            if (Tools.isNullOrSpace(gys.getGysid())) {
                gys.setGysid(Tools.newUUID());
            }
            manageService.saveGys(gys);

            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getGyses")
    @ResponseBody
    public HashMap getGyses() {
        HashMap hashMap = new HashMap();
        try {
            List<Gys> gyses = manageService.getGyses();
            if (gyses == null) {
                hashMap.put("msg", "empty");
            } else {
                for (Gys gys : gyses) {
                    if (!Tools.isNullOrSpace(gys.getLb())) {
                        String referdateNameByid = manageService.getCargocateNamwByid(gys.getLb());
                        gys.setLb(referdateNameByid);
                    }
                }
                hashMap.put("msg", "ok");
                hashMap.put("list", gyses);
            }
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getGysSel")
    @ResponseBody
    public HashMap getGysSel() {
        HashMap hashMap = new HashMap();
        List<NzSelOption> result = new ArrayList<>();
        try {
            List<Gys> gyses = manageService.getGyses();
            if (gyses == null) {
                hashMap.put("msg", "empty");
            } else {
                for (Gys gys : gyses) {
                    NzSelOption nzSelOption = new NzSelOption();
                    nzSelOption.setLabel(gys.getMc());
                    nzSelOption.setValue(gys.getGysid());
                    result.add(nzSelOption);
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", result);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteGysByid")
    @ResponseBody
    public HashMap deleteGysByid(@RequestBody Gys gys) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteGysByid(gys.getGysid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getGysByid")
    @ResponseBody
    public HashMap getGysByid(@RequestBody Gys gys) {
        HashMap hashMap = new HashMap();
        try {
            Gys gysByid = manageService.getGysByid(gys.getGysid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", gysByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getRefertypes")
    @ResponseBody
    public HashMap getRefertypes() {
        HashMap hashMap = new HashMap();
        try {
            List<Refertype> refertypes = manageService.getRefertypes();
            if (refertypes.isEmpty()) {
                hashMap.put("msg", "empty");
            } else {
                List<NzTreeVo> result = new ArrayList<>();
                for (Refertype refertype : refertypes) {
                    NzTreeVo v = new NzTreeVo();
                    v.setTitle(refertype.getName());
                    v.setKey(refertype.getRefertypeid());
                    v.setLeaf(true);
                    result.add(v);
                }
                hashMap.put("list", result);
                hashMap.put("msg", "ok");
            }

        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }

        return hashMap;
    }

    @PostMapping("getRefertypebyid")
    @ResponseBody
    public HashMap getRefertypebyid(@RequestBody Refertype refertype) {
        HashMap hashMap = new HashMap();
        try {
            Refertype refertypeByid = manageService.getRefertypeByid(refertype.getRefertypeid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", refertypeByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("saveRefertype")
    @ResponseBody
    public HashMap saveRefertype(@RequestBody Refertype refertype) {
        HashMap hashMap = new HashMap();
        try {

            if (Tools.isNullOrSpace(refertype.getRefertypeid())) {
                refertype.setRefertypeid(Tools.newUUID());
            }
            manageService.saveRefertype(refertype);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteRefertypebyid")
    @ResponseBody
    public HashMap deleteRefertypebyid(@RequestBody Refertype refertype) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteRefertypeByid(refertype.getRefertypeid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getReferdatasByTypeid")
    @ResponseBody
    public HashMap getReferdatasByTypeid(@RequestBody Referdata referdata) {
        HashMap hashMap = new HashMap();
        try {
            List<Referdata> referdatas = manageService.getReferdatasByTypeid(referdata.getTypeid());
            hashMap.put("list", referdatas);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("saveReferdata")
    @ResponseBody
    public HashMap saveReferdata(@RequestBody Referdata referdata) {
        HashMap hashMap = new HashMap();
        try {
            if (Tools.isNullOrSpace(referdata.getReferdata())) {
                referdata.setReferdata(Tools.newUUID());
            }
            Referdata referdata1 = manageService.saveReferdata(referdata);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getReferdatabyid")
    @ResponseBody
    public HashMap getReferdatabyid(@RequestBody Referdata referdata) {
        HashMap hashMap = new HashMap();
        try {
            Referdata referdataByid = manageService.getReferdataByid(referdata.getReferdata());
            hashMap.put("obj", referdataByid);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteReferdatabyid")
    @ResponseBody
    public HashMap deleteReferdatabyid(@RequestBody Referdata referdata) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteReferdataByid(referdata.getReferdata());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getReferdataSelectByTypeid")
    @ResponseBody
    public HashMap getReferdataSelectByTypeid(@RequestBody Referdata referdata) {
        HashMap hashMap = new HashMap();
        try {
            List<NzSelOption> referdatas = manageService.getReferdataSelectByTypeid(referdata.getTypeid());
            hashMap.put("list", referdatas);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getNztreeCargocate")
    @ResponseBody
    public HashMap getNztreeCargocate() {
        HashMap hashMap = new HashMap();
        try {
            Cargocate root = manageService.getCargocateByid("root");
            NzTreeVo rootv = new NzTreeVo();
            rootv.setKey(root.getCargocateid());
            rootv.setTitle(root.getName());
            rootv.setExpanded(true);
            List<NzTreeVo> cl = manageService.getNzTreeCargocate(root.getCargocateid());
            if (!cl.isEmpty()) {
                rootv.setChildren(cl);
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", rootv);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("saveCargocate")
    @ResponseBody
    public HashMap saveCargocate(@RequestBody Cargocate cargocate) {
        HashMap hashMap = new HashMap();
        try {
            if (Tools.isNullOrSpace(cargocate.getCargocateid())) {
                cargocate.setCargocateid(Tools.newUUID());
            }

            manageService.saveCargocate(cargocate);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteCargocate")
    @ResponseBody
    public HashMap deleteCargocate(@RequestBody Cargocate cargocate) {
        HashMap hashMap = new HashMap();
        try {
            if (cargocate.getCargocateid().equals("root")) {

            } else {
                List<Cargocate> cargocatesBypid = manageService.getCargocatesBypid(cargocate.getCargocateid());
                if (!cargocatesBypid.isEmpty()) {
                    for (Cargocate cargocate1 : cargocatesBypid) {
                        manageService.deleteCargocate(cargocate1.getCargocateid());
                    }
                }
                manageService.deleteCargocate(cargocate.getCargocateid());
            }
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getCargocateByid")
    @ResponseBody
    public HashMap getCargocateByid(@RequestBody Cargocate cargocate) {
        HashMap hashMap = new HashMap();
        try {
            Cargocate cargocateByid = manageService.getCargocateByid(cargocate.getCargocateid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", cargocateByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    /********************咖啡生豆入库管理*******************/
    @PostMapping("saveCoffeeincoming")
    @ResponseBody
    public HashMap saveCoffeeincoming(@RequestBody Coffeeincoming coffeeincoming) {
        HashMap hashMap = new HashMap();
        try {
            if (Tools.isNullOrSpace(coffeeincoming.getIncomingid())) {
                coffeeincoming.setIncomingid(Tools.newUUID());
                Rawbeaninventorychange task = new Rawbeaninventorychange();
                task.setBusid(coffeeincoming.getPm());
                task.setType(Tools.INVENTORY_CHANGE_TYPE_ADD);
                task.setChangetotal(coffeeincoming.getRksl());
                jmsTemplate.convertAndSend("rawBeanInventoryUpdate", task);
            }
            String currentUserId = manageService.getCurrentUserId();
            coffeeincoming.setCkczy(currentUserId);
            manageService.saveCoffeeincoming(coffeeincoming);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteCoffeeincomingByid")
    @ResponseBody
    public HashMap deleteCoffeeincomingByid(@RequestBody Coffeeincoming coffeeincoming) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteCoffeeincomingByid(coffeeincoming.getIncomingid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getCoffeeincomingByid")
    @ResponseBody
    public HashMap getCoffeeincomingByid(@RequestBody Coffeeincoming coffeeincoming) {
        HashMap hashMap = new HashMap();
        try {
            Coffeeincoming coffeeincomingByid = manageService.getCoffeeincomingByid(coffeeincoming.getIncomingid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", coffeeincomingByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getCoffeeincomings")
    @ResponseBody
    public HashMap getCoffeeincomings() {
        HashMap hashMap = new HashMap();
        try {
            List<Coffeeincoming> coffeeincomings = manageService.getCoffeeincomings();
            for (Coffeeincoming coffeeincoming : coffeeincomings) {
                if (!Tools.isNullOrSpace(coffeeincoming.getGys())) {
                    String referdateNameByid = manageService.getGysNameByid(coffeeincoming.getGys());

                    coffeeincoming.setGys(referdateNameByid);

                }
                if (!Tools.isNullOrSpace(coffeeincoming.getPm())) {
                    String referdateNameByid = manageService.getCargocateNamwByid(coffeeincoming.getPm());

                    coffeeincoming.setPm(referdateNameByid);

                }
                if (!Tools.isNullOrSpace(coffeeincoming.getCgry())) {
                    ;
                    Member memberByid = manageService.getMemberByid(coffeeincoming.getCgry());
                    String name = "";
                    if (memberByid != null) {
                        name = memberByid.getRealname();
                    }
                    coffeeincoming.setCgry(name);

                }
              /*  if (!Tools.isNullOrSpace(coffeeincoming.getQsfs())) {
                    String referdateNameByid = manageService.getReferdateNameByid(coffeeincoming.getQsfs());

                    coffeeincoming.setQsfs(referdateNameByid);

                }*/
            /*    if (!Tools.isNullOrSpace(coffeeincoming.getYtsm())) {
                    String referdateNameByid = manageService.getReferdateNameByid(coffeeincoming.getYtsm());

                    coffeeincoming.setYtsm(referdateNameByid);

                }*/
                if (!Tools.isNullOrSpace(coffeeincoming.getCkczy())) {

                    Member memberByid = manageService.getMemberByid(coffeeincoming.getCkczy());
                    String name = "";
                    if (memberByid != null) {
                        name = memberByid.getRealname();
                    }
                    coffeeincoming.setCkczy(name);

                }
               /* if (!Tools.isNullOrSpace(coffeeincoming.getJyhgz())) {
                    String jyhgzPic = Tools.BASE_IMG_PREFIX + coffeeincoming.getJyhgz();
                    coffeeincoming.setJyhgz(jyhgzPic);

                }
                if (!Tools.isNullOrSpace(coffeeincoming.getBgzm())) {
                    String BgzmPic = Tools.BASE_IMG_PREFIX + coffeeincoming.getBgzm();
                    coffeeincoming.setBgzm(BgzmPic);
                }*/
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", coffeeincomings);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    /********************咖啡生豆入库管理*******************/

   /* @PostMapping("uploadCoffeeincomingJyhgzPic")
    @ResponseBody
    public HashMap uploadCoffeeincomingJyhgzPic(HttpServletRequest request, @RequestParam("uploadfile") MultipartFile uploadfile) throws Exception {
        HashMap m = new HashMap();
        try {
            String finename = uploadfile.getOriginalFilename();
            String suffixname = uploadfile.getOriginalFilename().substring(finename.lastIndexOf("."));
            Boolean s = CanUpload.checkImg(suffixname);
            if (s == false) {
                m.put("msg", "imgerror");
                return m;
            }
            finename = String.valueOf(System.currentTimeMillis()) + suffixname;
            String incomingid = request.getHeader("id");
            File tf = new File(filepath);
            if (!tf.exists()) {
                tf.mkdir();
            }
            File savefile = new File(filepath + finename);
            uploadfile.transferTo(savefile);
            String picbase64 = BaseImgTools.imageToBase64Str(savefile.toString());
            if (!Tools.isNullOrSpace(picbase64)) {
                Coffeeincoming coffeeincomingByid = manageService.getCoffeeincomingByid(incomingid);
                coffeeincomingByid.setJyhgz(picbase64);
                manageService.saveCoffeeincoming(coffeeincomingByid);
                m.put("msg", "ok");
            } else {
                m.put("msg", "picerror");
            }
            savefile.delete();
            return m;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        m.put("msg", "error");
        return m;
    }

    @PostMapping("uploadCoffeeincomingBgzmPic")
    @ResponseBody
    public HashMap uploadCoffeeincomingBgzmPic(HttpServletRequest request, @RequestParam("uploadfile") MultipartFile uploadfile) throws Exception {
        HashMap m = new HashMap();
        try {
            String finename = uploadfile.getOriginalFilename();
            String suffixname = uploadfile.getOriginalFilename().substring(finename.lastIndexOf("."));
            Boolean s = CanUpload.checkImg(suffixname);
            if (s == false) {
                m.put("msg", "imgerror");
                return m;
            }
            finename = String.valueOf(System.currentTimeMillis()) + suffixname;
            String incomingid = request.getHeader("id");
            File tf = new File(filepath);
            if (!tf.exists()) {
                tf.mkdir();
            }
            File savefile = new File(filepath + finename);
            uploadfile.transferTo(savefile);
            String picbase64 = BaseImgTools.imageToBase64Str(savefile.toString());
            if (!Tools.isNullOrSpace(picbase64)) {
                Coffeeincoming coffeeincomingByid = manageService.getCoffeeincomingByid(incomingid);
                coffeeincomingByid.setBgzm(picbase64);
                manageService.saveCoffeeincoming(coffeeincomingByid);
                m.put("msg", "ok");
            } else {
                m.put("msg", "picerror");
            }
            savefile.delete();
            return m;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        m.put("msg", "error");
        return m;
    }*/

    /****************************Matincoming包材入庫管理*********************************/
    @PostMapping("saveMatincoming")
    @ResponseBody
    public HashMap saveMatincoming(@RequestBody Matincoming matincoming) {
        HashMap hashMap = new HashMap();
        try {
            if (Tools.isNullOrSpace(matincoming.getMatariaincomingid())) {
                matincoming.setMatariaincomingid(Tools.newUUID());
                Materiainventorychange task = new Materiainventorychange();
                task.setBusid(matincoming.getPm());
                task.setType(Tools.INVENTORY_CHANGE_TYPE_ADD);
                task.setChangetotal(matincoming.getSl());

                jmsTemplate.convertAndSend("materiaInventoryUpdate", task);

            }
            String currentUserId = manageService.getCurrentUserId();
            matincoming.setCkgly(currentUserId);
            manageService.saveMatincoming(matincoming);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteMatincomingbyid")
    @ResponseBody
    public HashMap deleteMatincomingbyid(@RequestBody Matincoming matincoming) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteMatincomingByid(matincoming.getMatariaincomingid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getMatincomingbyid")
    @ResponseBody
    public HashMap getMatincomingbyid(@RequestBody Matincoming matincoming) {
        HashMap hashMap = new HashMap();
        try {
            Matincoming matincomingByid = manageService.getMatincomingByid(matincoming.getMatariaincomingid());
            hashMap.put("obj", matincomingByid);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getMatincomings")
    @ResponseBody
    public HashMap getMatincomings() {
        HashMap hashMap = new HashMap();
        try {
            List<Matincoming> matincomings = manageService.getMatincomings();
            for (Matincoming matincoming : matincomings) {
                if (!Tools.isNullOrSpace(matincoming.getGys())) {
                    String referdateNameByid = manageService.getGysNameByid(matincoming.getGys());
                    matincoming.setGys(referdateNameByid);
                }
                if (!Tools.isNullOrSpace(matincoming.getGg())) {
                    String referdateNameByid = manageService.getReferdateNameByid(matincoming.getGg());

                    matincoming.setGg(referdateNameByid);

                }
                if (!Tools.isNullOrSpace(matincoming.getCgy())) {

                    String referdateNameByid = manageService.getMemberStrByid(matincoming.getCgy());

                    matincoming.setCgy(referdateNameByid);

                }
                if (!Tools.isNullOrSpace(matincoming.getCkgly())) {
                    String referdateNameByid = manageService.getMemberStrByid(matincoming.getCkgly());

                    matincoming.setCkgly(referdateNameByid);

                }
                if (!Tools.isNullOrSpace(matincoming.getPm())) {
                    String referdateNameByid = manageService.getCargocateNamwByid(matincoming.getPm());

                    matincoming.setPm(referdateNameByid);

                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", matincomings);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }
    /****************************Matincoming包材入库管理*********************************/

    /****************************Proplan生产计划管理*********************************/
    @PostMapping("saveProplan")
    @ResponseBody
    public HashMap saveProplan(@RequestBody Proplan proplan) {
        HashMap hashMap = new HashMap();
        try {
            if (Tools.isNullOrSpace(proplan.getProplanid())) {
                proplan.setProplanid(Tools.newUUID());
            }
            manageService.saveProplan(proplan);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteProplanByid")
    @ResponseBody
    public HashMap deleteProplanByid(@RequestBody Proplan proplan) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteProplanByid(proplan.getProplanid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getProplanByid")
    @ResponseBody
    public HashMap getProplanByid(@RequestBody Proplan proplan) {
        HashMap hashMap = new HashMap();
        try {
            Proplan proplanByid = manageService.getProplanByid(proplan.getProplanid());
            hashMap.put("obj", proplanByid);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getProplans")
    @ResponseBody
    public HashMap getProplans() {
        HashMap hashMap = new HashMap();
        try {
            List<Proplan> proplans = manageService.getProplans();
            hashMap.put("msg", "ok");
            hashMap.put("list", proplans);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }
/****************************Proplan生产计划管理*********************************/

    /*********************Dbd出库管理*************************/
    @PostMapping("saveDbd")
    @ResponseBody
    public HashMap saveDbd(@RequestBody Dbd dbd) {
        HashMap hashMap = new HashMap();
        try {
            if (Tools.isNullOrSpace(dbd.getDbdid())) {
//               生产计划里的 生豆/包材出库请求处理
                Dbd dbdByScpc = new Dbd();
                if(dbd.getType().equals("咖啡")){
                      dbdByScpc = manageService.getDbdByScpcAndCoffee(dbd.getScpc());
                }else{
                    dbdByScpc = manageService.getDbdByScpcAndBC(dbd.getScpc());
                }

                if(dbdByScpc == null){
                     dbdByScpc = new Dbd();
                    dbdByScpc.setDbdid(Tools.newUUID());
                    dbdByScpc.setPm(dbd.getPm());
                    dbdByScpc.setScpc(dbd.getScpc());
                    dbdByScpc.setType(dbd.getType());
                    dbdByScpc.setSqsl(dbd.getSqsl());
                }
                else{
                    dbdByScpc.setPm(dbd.getPm());
                    dbdByScpc.setSqsl(dbd.getSqsl());
                }

                dbdByScpc.setClqk("WCL");

                manageService.saveDbd(dbdByScpc);
            } else {
//                出库单调拨单设置
                //烘焙记录表是否增加
                Bakerecord bakerecordByScpc = manageService.getBakerecordByScpc(dbd.getScpc());
                if (bakerecordByScpc == null) {
                    Bakerecord bakerecord = new Bakerecord();
                    bakerecord.setBakerecordid(Tools.newUUID());
                    bakerecord.setDbid(dbd.getDbdid());
                    bakerecord.setClqk("WCL");
                    bakerecord.setScpc(dbd.getScpc());
                    manageService.saveBakerecord(bakerecord);

                }

                dbd.setClqk("YCL");
                Proplan proplanByid = manageService.getProplanByid(dbd.getScpc());
                if (dbd.getSddbsl() == null) {
//                    包材
                    proplanByid.setSjbcsl(dbd.getBcdbsl());

//                    库存总表
                    Materiainventory materiainventoryByPm = manageService.getMateriainventoryByPm(dbd.getPm());
                    materiainventoryByPm.setTotal(materiainventoryByPm.getTotal() - dbd.getBcdbsl());
                    manageService.saveMateriainventory(materiainventoryByPm);
                    Materiainventorychange materiainventorychange = new Materiainventorychange();
                    materiainventorychange.setMateriainventorychangeid(Tools.newUUID());
                    materiainventorychange.setChangetotal(dbd.getBcdbsl());
                    materiainventorychange.setBusid(dbd.getPm());
                    materiainventorychange.setChangedate(new Timestamp(System.currentTimeMillis()));
                    materiainventorychange.setType(Tools.INVENTORY_CHANGE_TYPE_OUT);
                    manageService.saveMateriainventorychange(materiainventorychange);
                } else {
                    //                 咖啡

//                    Float temp = dbd.getSddbsl().floatValue();
                    proplanByid.setSjsdyl(dbd.getSddbsl());

                    //库存总表
                    Rawbeaninventory rawbeaninventoryByPmId = manageService.getRawbeaninventoryByPmId(dbd.getPm());
                    rawbeaninventoryByPmId.setTotal(rawbeaninventoryByPmId.getTotal() - dbd.getSddbsl());
                    manageService.saveRawbeaninventory(rawbeaninventoryByPmId);

                    Rawbeaninventorychange rawchange = new Rawbeaninventorychange();
                    rawchange.setRawbeaninventorychangeid(Tools.newUUID());
                    rawchange.setChangetotal(dbd.getSddbsl());
                    rawchange.setBusid(dbd.getPm());
                    rawchange.setChangedate(new Timestamp(System.currentTimeMillis()));
                    rawchange.setType(Tools.INVENTORY_CHANGE_TYPE_OUT);
                    manageService.saveRawbeaninventorychange(rawchange);


                    BlockChain blockChain = new BlockChain();
         /*   Dbd dbdByScpcAndCoffee = manageService.getDbdByScpcAndCoffee(dbd.getScpc());
            if(dbdByScpcAndCoffee!=null){*/
                    blockChain.setScpcid(dbd.getScpc());

                    if (proplanByid != null) {
                        if (Tools.isNullOrSpace(proplanByid.getScmc())) {
                            blockChain.setScpcmc("");
                        } else {
                            blockChain.setScpcmc(proplanByid.getScmc());
                        }
                    }

                    Coffeeincoming coffeeincomingByid = manageService.getCoffeeincomingByPm(dbd.getPm());
                    String referdateNameByid = manageService.getGysNameByid(coffeeincomingByid.getGys());
                    blockChain.setYcljhcj(referdateNameByid);
//            }
                    String key = dbd.getScpc();
                    String jsonStr = "";
                    jsonStr = new Gson().toJson(blockChain);

                    boolean flag = blockAdminService.invokeByJson(key, jsonStr);
                    if (flag) {
                        Blockchaindb blockchaindb = manageService.getBlockchaindbByKeyname(dbd.getScpc());
                        if (blockchaindb == null) {
                            blockchaindb = new Blockchaindb();
                            blockchaindb.setKeyname(dbd.getScpc());
                            blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                        } else {
                            blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                        }
                        manageService.saveBlockchaindb(blockchaindb);

                    }
                }
                String currentUserId = manageService.getCurrentUserId();
                dbd.setCkr(currentUserId);
                manageService.saveDbd(dbd);
            }



            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteDbdByid")
    @ResponseBody
    public HashMap deleteDbdByid(@RequestBody Dbd dbd) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteDbdByid(dbd.getDbdid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }


    @PostMapping("getDbdByid")
    @ResponseBody
    public HashMap getDbdByid(@RequestBody Dbd dbd) {
        HashMap hashMap = new HashMap();
        try {
            Dbd dbdByid = manageService.getDbdByid(dbd.getDbdid());
            hashMap.put("obj", dbdByid);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getDbds")
    @ResponseBody
    public HashMap getDbds() {
        HashMap hashMap = new HashMap();
        try {
            List<Dbd> dbds = manageService.getDbds();
            for (Dbd dbd : dbds) {
                if (!Tools.isNullOrSpace(dbd.getCkr())) {
                    String referdateNameByid = manageService.getMemberStrByid(dbd.getCkr());
                    dbd.setCkr(referdateNameByid);
                }
                if (!Tools.isNullOrSpace(dbd.getPm())) {
                    String cargocateNamwByid = manageService.getCargocateNamwByid(dbd.getPm());
                    dbd.setPm(cargocateNamwByid);
                }
              /*  if (!Tools.isNullOrSpace(dbd.getDbry())) {
                    String referdateNameByid = manageService.getMemberStrByid(dbd.getDbry());
                    dbd.setDbry(referdateNameByid);
                }
                if (!Tools.isNullOrSpace(dbd.getDbdw())) {
                    String referdateNameByid = manageService.getReferdateNameByid(dbd.getDbdw());
                    dbd.setDbdw(referdateNameByid);
                }*/
                if (!Tools.isNullOrSpace(dbd.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(dbd.getScpc());
                    if (proplanByid != null)
                        dbd.setScpc(proplanByid.getScpch());
                }

                if (dbd.getClqk().equals("WCL")) {
                    dbd.setClqk("未处理");
                } else {
                    dbd.setClqk("已处理");
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", dbds);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getDbdsByClqk")
    @ResponseBody
    public HashMap getDbdsByClqk(@RequestBody Dbd dbdtemp) {
        HashMap hashMap = new HashMap();
        try {
            List<Dbd> dbds = manageService.getDbdsByClqk(dbdtemp.getClqk());
            for (Dbd dbd : dbds) {
                if (!Tools.isNullOrSpace(dbd.getCkr())) {
                    String referdateNameByid = manageService.getMemberStrByid(dbd.getCkr());
                    dbd.setCkr(referdateNameByid);
                }
                if (!Tools.isNullOrSpace(dbd.getPm())) {
                    String cargocateNamwByid = manageService.getCargocateNamwByid(dbd.getPm());
                    dbd.setPm(cargocateNamwByid);
                }
               /* if (!Tools.isNullOrSpace(dbd.getDbry())) {
                    String referdateNameByid = manageService.getMemberStrByid(dbd.getDbry());
                    dbd.setDbry(referdateNameByid);
                }
                if (!Tools.isNullOrSpace(dbd.getDbdw())) {
                    String referdateNameByid = manageService.getReferdateNameByid(dbd.getDbdw());
                    dbd.setDbdw(referdateNameByid);
                }*/
                if (!Tools.isNullOrSpace(dbd.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(dbd.getScpc());
                    if (proplanByid != null)
                        dbd.setScpc(proplanByid.getScpch());
                }

                if (dbd.getClqk().equals("WCL")) {
                    dbd.setClqk("未处理");
                } else {
                    dbd.setClqk("已处理");
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", dbds);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    /*********************Dbd出库管理*************************/

    @PostMapping("getNzSelCateByTypeid")
    @ResponseBody
    public HashMap getNzSelCateByTypeid(@RequestBody Cargocate cargocate) {
        HashMap hashMap = new HashMap();
        try {
            List<NzSelOption> nzTreeCargocate = manageService.getNzSelOptionCargocate(cargocate.getCargocateid());
            hashMap.put("msg", "ok");
            hashMap.put("list", nzTreeCargocate);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    /*********************Bakerrecord烘焙生产*************/
    @GetMapping("getBakerecord")
    @ResponseBody
    public HashMap getBakerecord() {
        HashMap hashMap = new HashMap();
        try {
            List<Bakerecord> bakerecords = manageService.getBakerecords();
            for (Bakerecord bakerecord : bakerecords) {
                if (!Tools.isNullOrSpace(bakerecord.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(bakerecord.getScpc());
                    if (proplanByid != null)
                        bakerecord.setScpc(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(bakerecord.getClqk())) {
                    if (bakerecord.getClqk().equals("WCL"))
                        bakerecord.setClqk("未处理");
                    else
                        bakerecord.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(bakerecord.getHbr())) {
                    String referdateNameByid = manageService.getMemberStrByid(bakerecord.getHbr());
                    bakerecord.setHbr(referdateNameByid);
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", bakerecords);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }


    @PostMapping("getBakerecordsByClqk")
    @ResponseBody
    public HashMap getBakerecordsByClqk(@RequestBody Bakerecord bakerecord1) {
        HashMap hashMap = new HashMap();
        try {
            List<Bakerecord> bakerecords = manageService.getBakerecordsByClqk(bakerecord1.getClqk());
            for (Bakerecord bakerecord : bakerecords) {
                if (!Tools.isNullOrSpace(bakerecord.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(bakerecord.getScpc());
                    if (proplanByid != null)
                        bakerecord.setScpc(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(bakerecord.getClqk())) {
                    if (bakerecord.getClqk().equals("WCL"))
                        bakerecord.setClqk("未处理");
                    else
                        bakerecord.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(bakerecord.getHbr())) {
                    String referdateNameByid = manageService.getReferdateNameByid(bakerecord.getHbr());
                    bakerecord.setHbr(referdateNameByid);
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", bakerecords);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }


    @PostMapping("saveBakerecord")
    @ResponseBody
    public HashMap saveBakerecord(@RequestBody Bakerecord bakerecord) {
        HashMap hashMap = new HashMap();
        try {
            bakerecord.setClqk("YCL");
            bakerecord.setHbr(manageService.getCurrentUserId());
            manageService.saveBakerecord(bakerecord);
//            检验
            Baketest baketestByScpc = manageService.getBaketestByScpc(bakerecord.getScpc());
            if (baketestByScpc == null) {
                Baketest baketest = new Baketest();
                baketest.setBakerecordid(bakerecord.getBakerecordid());
                baketest.setBaketestid(Tools.newUUID());
                baketest.setScpc(bakerecord.getScpc());
                baketest.setClqk("WCL");
                manageService.saveBaketest(baketest);
            }
//           杯测
            Cuptest cuptestByScpc = manageService.getCuptestByScpc(bakerecord.getScpc());
            if (cuptestByScpc == null) {
                Cuptest cuptest = new Cuptest();
                cuptest.setCuptestid(Tools.newUUID());
                cuptest.setBakeid(bakerecord.getBakerecordid());
                cuptest.setScpcid(bakerecord.getScpc());
                cuptest.setClqk("WCL");
                manageService.saveCuptest(cuptest);
            }
//           回写烘焙单号
            Proplan proplanByid = manageService.getProplanByid(bakerecord.getScpc());
//            proplanByid.setHbdh(bakerecord.getBakerecordid());
            manageService.saveProplan(proplanByid);


            BlockChain blockChain = new BlockChain();
//            Bakerecord bakerecordByScpc = manageService.getBakerecordByScpc(bakerecord.getScpc());


            blockChain.setScpcid(bakerecord.getScpc());
            if (proplanByid != null) {
                if (Tools.isNullOrSpace(proplanByid.getScmc())) {
                    blockChain.setScpcmc("");
                } else {
                    blockChain.setScpcmc(proplanByid.getScmc());
                }
            }

            //生豆入库管理表
            Dbd dbdByScpcAndCoffee = manageService.getDbdByScpcAndCoffee(bakerecord.getScpc());
            Coffeeincoming coffeeincomingByid = manageService.getCoffeeincomingByPm(dbdByScpcAndCoffee.getPm());

            String currentDate = Tools.getCurrentDate();
            Long preMonth = Tools.getPreMonth(currentDate);

            String referdateNameByid = manageService.getGysNameByid(coffeeincomingByid.getGys());
            blockChain.setYcljhcj(referdateNameByid);

            blockChain.setRdwd(String.valueOf(bakerecord.getRdwd()));
            blockChain.setHwwd(String.valueOf(bakerecord.getHwwd()));
            blockChain.setYbwd(String.valueOf(bakerecord.getYbwd()));
            blockChain.setYbsj(String.valueOf(bakerecord.getYbsj()));
            blockChain.setCdwd(String.valueOf(bakerecord.getCdwd()));
            blockChain.setFm(String.valueOf(bakerecord.getFm()));
            blockChain.setHl(String.valueOf(bakerecord.getHl()));
            blockChain.setHj(bakerecord.getHj());
            blockChain.setHbr(manageService.getMemberStrByid(bakerecord.getHbr()));
            blockChain.setHbrq(bakerecord.getHbrq().getTime());
            String key = bakerecord.getScpc();
            String jsonStr = "";
            jsonStr = new Gson().toJson(blockChain);

            boolean flag = blockAdminService.invokeByJson(key, jsonStr);
            if (flag) {
                Blockchaindb blockchaindb = manageService.getBlockchaindbByKeyname(bakerecord.getScpc());
                if (blockchaindb == null) {
                    blockchaindb = new Blockchaindb();
                    blockchaindb.setKeyname(bakerecord.getScpc());
                    blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                } else {
                    blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                }
                manageService.saveBlockchaindb(blockchaindb);
            }
            hashMap.put("msg", "ok");

        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteBakerecordByid")
    @ResponseBody
    public HashMap deleteBakerecordByid(@RequestBody Bakerecord bakerecord) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteBakerecordByid(bakerecord.getBakerecordid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getBakerecordByid")
    @ResponseBody
    public HashMap getBakerecordByid(@RequestBody Bakerecord bakerecord) {
        HashMap hashMap = new HashMap();
        try {
            Bakerecord bakerecordByid = manageService.getBakerecordByid(bakerecord.getBakerecordid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", bakerecordByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }
    /*********************Bakerrecord烘焙生产*************/
    /*************************Baketest检验*******************************/
    @GetMapping("getBaketests")
    @ResponseBody
    public HashMap getBaketests() {
        HashMap hashMap = new HashMap();
        try {
            List<Baketest> baketests = manageService.getBaketests();
            for (Baketest baketest : baketests) {
                if (!Tools.isNullOrSpace(baketest.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(baketest.getScpc());
                    if (proplanByid != null)
                        baketest.setScpc(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(baketest.getJcr())) {
                    String referdateNameByid = manageService.getMemberStrByid(baketest.getJcr());
                    baketest.setJcr(referdateNameByid);
                }
                if (!Tools.isNullOrSpace(baketest.getClqk())) {
                    if (baketest.getClqk().equals("WCL"))
                        baketest.setClqk("未处理");
                    else
                        baketest.setClqk("已处理");
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", baketests);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    //    getBaketestsByClqk
    @PostMapping("getBaketestsByClqk")
    @ResponseBody
    public HashMap getBaketestsByClqk(@RequestBody Baketest baketest1) {
        HashMap hashMap = new HashMap();
        try {
            List<Baketest> baketests = manageService.getBaketestsByClqk(baketest1.getClqk());
            for (Baketest baketest : baketests) {
                if (!Tools.isNullOrSpace(baketest.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(baketest.getScpc());
                    if (proplanByid != null)
                        baketest.setScpc(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(baketest.getJcr())) {
                    String referdateNameByid = manageService.getReferdateNameByid(baketest.getJcr());
                    baketest.setJcr(referdateNameByid);
                }
                if (!Tools.isNullOrSpace(baketest.getClqk())) {
                    if (baketest.getClqk().equals("WCL"))
                        baketest.setClqk("未处理");
                    else
                        baketest.setClqk("已处理");
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", baketests);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("saveBaketest")
    @ResponseBody
    public HashMap saveBaketest(@RequestBody Baketest baketest) {
        HashMap hashMap = new HashMap();
        try {
            baketest.setClqk("YCL");
            baketest.setJcr(manageService.getCurrentUserId());
            manageService.saveBaketest(baketest);

            Proplan proplanByid = manageService.getProplanByid(baketest.getScpc());
//            proplanByid.setJydh(baketest.getBaketestid());
            manageService.saveProplan(proplanByid);

            Encapsort encapsort = new Encapsort();
            encapsort.setEncapsortid(Tools.newUUID());
            encapsort.setScpc(baketest.getScpc());
            encapsort.setClqk("WCL");

            Encapsort encapsortByScpc = manageService.getEncapsortByScpc(baketest.getScpc());
            if (encapsortByScpc == null)
                manageService.saveEncapsort(encapsort);

            BlockChain blockChain = new BlockChain();
//            Baketest baketestByScpc = manageService.getBaketestByScpc(baketest.getScpc());

            //生产计划表
            blockChain.setScpcid(baketest.getScpc());
            if (proplanByid != null) {
                if (Tools.isNullOrSpace(proplanByid.getScmc())) {
                    blockChain.setScpcmc("");
                } else {
                    blockChain.setScpcmc(proplanByid.getScmc());
                }
            }

            //生豆入库管理表
            Dbd dbdByScpcAndCoffee = manageService.getDbdByScpcAndCoffee(baketest.getScpc());
            Coffeeincoming coffeeincomingByid = manageService.getCoffeeincomingByPm(dbdByScpcAndCoffee.getPm());

            String currentDate = Tools.getCurrentDate();
            Long preMonth = Tools.getPreMonth(currentDate);

            String referdateNameByid = manageService.getGysNameByid(coffeeincomingByid.getGys());
            blockChain.setYcljhcj(referdateNameByid);

            //烘焙生产表        BlockChain blockChain = new BlockChain();

            Bakerecord bakerecordByScpc = manageService.getBakerecordByScpc(baketest.getScpc());
            blockChain.setRdwd(String.valueOf(bakerecordByScpc.getRdwd()));
            blockChain.setHwwd(String.valueOf(bakerecordByScpc.getHwwd()));
            blockChain.setYbwd(String.valueOf(bakerecordByScpc.getYbwd()));
            blockChain.setYbsj(String.valueOf(bakerecordByScpc.getYbsj()));
            blockChain.setCdwd(String.valueOf(bakerecordByScpc.getCdwd()));
            blockChain.setFm(String.valueOf(bakerecordByScpc.getFm()));
            blockChain.setHl(String.valueOf(bakerecordByScpc.getHl()));
            blockChain.setHj(bakerecordByScpc.getHj());
            blockChain.setHbr(manageService.getMemberStrByid(bakerecordByScpc.getHbr()));
            blockChain.setHbrq(bakerecordByScpc.getHbrq().getTime());

            blockChain.setQyrqsj(baketest.getQysj().getTime());
            blockChain.setSz(String.valueOf(baketest.getSz()));
            blockChain.setSf(String.valueOf(baketest.getSf()));
            blockChain.setJcr(manageService.getMemberStrByid(baketest.getJcr()));


            String key = baketest.getScpc();
            String jsonStr = "";
            jsonStr = new Gson().toJson(blockChain);

            boolean flag = blockAdminService.invokeByJson(key, jsonStr);
            if (flag) {
                Blockchaindb blockchaindb = manageService.getBlockchaindbByKeyname(baketest.getScpc());
                if (blockchaindb == null) {
                    blockchaindb = new Blockchaindb();
                    blockchaindb.setKeyname(baketest.getScpc());
                    blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                } else {
                    blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                }
                manageService.saveBlockchaindb(blockchaindb);

            }
            hashMap.put("msg", "ok");

        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteBaketestByid")
    @ResponseBody
    public HashMap deleteBaketestByid(@RequestBody Baketest baketest) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteBaketestByid(baketest.getBaketestid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getBaketestByid")
    @ResponseBody
    public HashMap getBakerecordByid(@RequestBody Baketest baketest) {
        HashMap hashMap = new HashMap();
        try {
            Baketest baketestByid = manageService.getBaketestByid(baketest.getBaketestid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", baketestByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }
    /*************************Baketest检验*******************************/

    /*************************Encapsort分拣*******************************/
    @GetMapping("getEncapsorts")
    @ResponseBody
    public HashMap getEncapsorts() {
        HashMap hashMap = new HashMap();
        try {
            List<Encapsort> baketests = manageService.getEncapsorts();
            for (Encapsort baketest : baketests) {
                if (!Tools.isNullOrSpace(baketest.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(baketest.getScpc());
                    if (proplanByid != null)
                        baketest.setScpc(proplanByid.getScpch());
                }

                if (!Tools.isNullOrSpace(baketest.getClqk())) {
                    if (baketest.getClqk().equals("WCL"))
                        baketest.setClqk("未处理");
                    else
                        baketest.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(baketest.getFjy())) {
                    baketest.setFjy(manageService.getMemberStrByid(baketest.getFjy()));
                }

            }
            hashMap.put("msg", "ok");
            hashMap.put("list", baketests);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("saveEncapsort")
    @ResponseBody
    public HashMap saveEncapsort(@RequestBody Encapsort baketest) {
        HashMap hashMap = new HashMap();
        try {
            Encapsulation encapsulation = new Encapsulation();
            encapsulation.setEncapsulationsortid(baketest.getEncapsortid());
            encapsulation.setClqk("WCL");
            encapsulation.setScpc(baketest.getScpc());
            encapsulation.setBakebeantotal(baketest.getHgsl());

            encapsulation.setEncapsulationid(Tools.newUUID());
            Dbd dbdByScpc = manageService.getDbdByScpcAndBC(baketest.getScpc());
            encapsulation.setBcdbdid(dbdByScpc.getDbdid());
            String cargocateNamwByid = manageService.getCargocateNamwByid(dbdByScpc.getPm());
            encapsulation.setBcmc(cargocateNamwByid);
            encapsulation.setBcsl(dbdByScpc.getBcdbsl());

            Encapsulation encapsulationByScpc = manageService.getEncapsulationByScpc(baketest.getScpc());
            if (encapsulationByScpc == null)
                manageService.saveEncapsulation(encapsulation);

            baketest.setClqk("YCL");
            baketest.setFjy(manageService.getCurrentUserId());
            manageService.saveEncapsort(baketest);

            Proplan proplanByid = manageService.getProplanByid(baketest.getScpc());
//            proplanByid.setFjdh(baketest.getEncapsortid());

            manageService.saveProplan(proplanByid);

            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteEncapsortByid")
    @ResponseBody
    public HashMap deleteEncapsortByid(@RequestBody Encapsort baketest) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteBaketestByid(baketest.getEncapsortid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getEncapsortByid")
    @ResponseBody
    public HashMap getEncapsortByid(@RequestBody Encapsort baketest) {
        HashMap hashMap = new HashMap();
        try {
            Encapsort baketestByid = manageService.getEncapsortByid(baketest.getEncapsortid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", baketestByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getEncapsortByClqk")
    @ResponseBody
    public HashMap getEncapsortByClqk(@RequestBody Encapsort encapsort) {
        HashMap hashMap = new HashMap();
        try {
            List<Encapsort> baketests = manageService.getEncapsortsByClqk(encapsort.getClqk());
            for (Encapsort baketest : baketests) {
                if (!Tools.isNullOrSpace(baketest.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(baketest.getScpc());
                    if (proplanByid != null)
                        baketest.setScpc(proplanByid.getScpch());
                }

                if (!Tools.isNullOrSpace(baketest.getClqk())) {
                    if (baketest.getClqk().equals("WCL"))
                        baketest.setClqk("未处理");
                    else
                        baketest.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(baketest.getFjy())) {
                    baketest.setFjy(manageService.getReferdateNameByid(baketest.getFjy()));
                }

            }
            hashMap.put("msg", "ok");
            hashMap.put("list", baketests);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }
    /*************************Encapsort分拣*******************************/

    /************************Cuptest杯测**********************/
    @GetMapping("getCuptests")
    @ResponseBody
    public HashMap getCuptests() {
        HashMap hashMap = new HashMap();
        try {
            List<Cuptest> baketests = manageService.getCuptests();
            for (Cuptest baketest : baketests) {
                if (!Tools.isNullOrSpace(baketest.getScpcid())) {
                    Proplan proplanByid = manageService.getProplanByid(baketest.getScpcid());
                    if (proplanByid != null)
                        baketest.setScpcid(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(baketest.getBcdj())) {
                    String referdateNameByid = manageService.getReferdateNameByid(baketest.getBcdj());
                    baketest.setBcdj(referdateNameByid);
                }
                if (!Tools.isNullOrSpace(baketest.getClqk())) {
                    if (baketest.getClqk().equals("WCL"))
                        baketest.setClqk("未处理");
                    else
                        baketest.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(baketest.getBcry())) {
                    baketest.setBcry(manageService.getMemberStrByid(baketest.getBcry()));
                }

            }
            hashMap.put("msg", "ok");
            hashMap.put("list", baketests);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("saveCuptest")
    @ResponseBody
    public HashMap saveCuptest(@RequestBody Cuptest cuptest) {
        HashMap hashMap = new HashMap();
        try {
            Encapsort encapsort = new Encapsort();
            encapsort.setScpc(cuptest.getScpcid());
            encapsort.setClqk("WCL");
            encapsort.setEncapsortid(Tools.newUUID());
            Encapsort encapsortByScpc = manageService.getEncapsortByScpc(cuptest.getScpcid());
            if (encapsortByScpc == null)
                manageService.saveEncapsort(encapsort);

            cuptest.setClqk("YCL");
            cuptest.setBcry(manageService.getCurrentUserId());
            manageService.saveCuptest(cuptest);

            Proplan proplanByid = manageService.getProplanByid(cuptest.getScpcid());
//            proplanByid.setBcdh(cuptest.getCuptestid());
            manageService.saveProplan(proplanByid);

            BlockChain blockChain = new BlockChain();
//            Cuptest cuptestByScpc = manageService.getCuptestByScpc(cuptest.getScpcid());

            //生产计划表
            blockChain.setScpcid(cuptest.getScpcid());
            if (proplanByid != null) {
                if (Tools.isNullOrSpace(proplanByid.getScmc())) {
                    blockChain.setScpcmc("");
                } else {
                    blockChain.setScpcmc(proplanByid.getScmc());
                }
            }

            //生豆入库管理表
            Dbd dbdByScpcAndCoffee = manageService.getDbdByScpcAndCoffee(cuptest.getScpcid());
            Coffeeincoming coffeeincomingByid = manageService.getCoffeeincomingByPm(dbdByScpcAndCoffee.getPm());

            String currentDate = Tools.getCurrentDate();
            Long preMonth = Tools.getPreMonth(currentDate);

            String referdateNameByid = manageService.getGysNameByid(coffeeincomingByid.getGys());
            blockChain.setYcljhcj(referdateNameByid);

            //烘焙生产表        BlockChain blockChain = new BlockChain();

            Bakerecord bakerecordByScpc = manageService.getBakerecordByScpc(cuptest.getScpcid());
            blockChain.setRdwd(String.valueOf(bakerecordByScpc.getRdwd()));
            blockChain.setHwwd(String.valueOf(bakerecordByScpc.getHwwd()));
            blockChain.setYbwd(String.valueOf(bakerecordByScpc.getYbwd()));
            blockChain.setYbsj(String.valueOf(bakerecordByScpc.getYbsj()));
            blockChain.setCdwd(String.valueOf(bakerecordByScpc.getCdwd()));
            blockChain.setFm(String.valueOf(bakerecordByScpc.getFm()));
            blockChain.setHl(String.valueOf(bakerecordByScpc.getHl()));
            blockChain.setHj(bakerecordByScpc.getHj());
            blockChain.setHbr(manageService.getMemberStrByid(bakerecordByScpc.getHbr()));
            blockChain.setHbrq(bakerecordByScpc.getHbrq().getTime());

            //检验表
            Baketest baketestByScpc = manageService.getBaketestByScpc(cuptest.getScpcid());
            blockChain.setQyrqsj(baketestByScpc.getQysj().getTime());
            blockChain.setSz(String.valueOf(baketestByScpc.getSz()));
            blockChain.setSf(String.valueOf(baketestByScpc.getSf()));
            blockChain.setJcr(manageService.getMemberStrByid(baketestByScpc.getJcr()));

            blockChain.setHbd(String.valueOf(cuptest.getHbd()));
            blockChain.setBsj(cuptest.getBcsj().getTime());
            blockChain.setBcry(manageService.getMemberStrByid(cuptest.getBcry()));
            blockChain.setGx(String.valueOf(cuptest.getGx()));
            blockChain.setSx(String.valueOf(cuptest.getSx()));
            blockChain.setFw(String.valueOf(cuptest.getFw()));
            blockChain.setHg(String.valueOf(cuptest.getHg()));
            blockChain.setSd(String.valueOf(cuptest.getSd()));
            blockChain.setChd(String.valueOf(cuptest.getChd()));
            blockChain.setYzx(String.valueOf(cuptest.getYzx()));
            blockChain.setPhg(String.valueOf(cuptest.getPhg()));
            blockChain.setGjd(String.valueOf(cuptest.getGjd()));
            blockChain.setTd(String.valueOf(cuptest.getTd()));
            blockChain.setZtfz(String.valueOf(cuptest.getTotal()));
            blockChain.setBcpj(manageService.getReferdateNameByid(cuptest.getBcdj()));
            String key = cuptest.getScpcid();
            String jsonStr = "";
            jsonStr = new Gson().toJson(blockChain);
            boolean flag = blockAdminService.invokeByJson(key, jsonStr);
            if (flag) {
                Blockchaindb blockchaindb = manageService.getBlockchaindbByKeyname(cuptest.getScpcid());
                if (blockchaindb == null) {
                    blockchaindb = new Blockchaindb();
                    blockchaindb.setKeyname(cuptest.getScpcid());
                    blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                } else {
                    blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                }
                manageService.saveBlockchaindb(blockchaindb);

            }
            hashMap.put("msg", "ok");

        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteCuptestByid")
    @ResponseBody
    public HashMap deleteCuptestByid(@RequestBody Cuptest cuptest) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteCuptestByid(cuptest.getCuptestid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getCuptestByid")
    @ResponseBody
    public HashMap getCuptestByid(@RequestBody Cuptest cuptest) {
        HashMap hashMap = new HashMap();
        try {
            Cuptest baketestByid = manageService.getCuptestByid(cuptest.getCuptestid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", baketestByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getCuptestByClqk")
    @ResponseBody
    public HashMap getCuptestByClqk(@RequestBody Cuptest cuptest) {
        HashMap hashMap = new HashMap();
        try {
            List<Cuptest> baketests = manageService.getCuptestsByClqk(cuptest.getClqk());
            for (Cuptest baketest : baketests) {
                if (!Tools.isNullOrSpace(baketest.getScpcid())) {
                    Proplan proplanByid = manageService.getProplanByid(baketest.getScpcid());
                    if (proplanByid != null)
                        baketest.setScpcid(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(baketest.getBcdj())) {
                    String referdateNameByid = manageService.getReferdateNameByid(baketest.getBcdj());
                    baketest.setBcdj(referdateNameByid);
                }

                if (!Tools.isNullOrSpace(baketest.getClqk())) {
                    if (baketest.getClqk().equals("WCL"))
                        baketest.setClqk("未处理");
                    else
                        baketest.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(baketest.getBcry())) {
                    baketest.setBcry(manageService.getReferdateNameByid(baketest.getBcry()));
                }

            }
            hashMap.put("msg", "ok");
            hashMap.put("list", baketests);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }
    /************************Cuptest杯测**********************/

    /************************Encapsulation杯测**********************/
    @GetMapping("getEncapsulations")
    @ResponseBody
    public HashMap getEncapsulations() {
        HashMap hashMap = new HashMap();
        try {
            List<Encapsulation> encapsulations = manageService.getEncapsulations();
            for (Encapsulation encapsulation : encapsulations) {
                if (!Tools.isNullOrSpace(encapsulation.getClqk())) {
                    if (encapsulation.getClqk().equals("WCL"))
                        encapsulation.setClqk("未处理");
                    else
                        encapsulation.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(encapsulation.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(encapsulation.getScpc());
                    if (proplanByid != null)
                        encapsulation.setScpc(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(encapsulation.getFzry())) {
                    String referdateNameByid = manageService.getMemberStrByid(encapsulation.getFzry());
                    encapsulation.setFzry(referdateNameByid);
                }
                if (!Tools.isNullOrSpace(encapsulation.getProid())) {
                    String cargocateNamwByid = manageService.getCargocateNamwByid(encapsulation.getProid());
                    encapsulation.setProid(cargocateNamwByid);
                }

            }
            hashMap.put("msg", "ok");
            hashMap.put("list", encapsulations);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getEncapsulationsByClqk")
    @ResponseBody
    public HashMap getEncapsulationsByClqk(@RequestBody Encapsulation encapsulation1) {
        HashMap hashMap = new HashMap();
        try {
            List<Encapsulation> encapsulations = manageService.getEncapsulationsByClqk(encapsulation1.getClqk());
            for (Encapsulation encapsulation : encapsulations) {
                if (!Tools.isNullOrSpace(encapsulation.getClqk())) {
                    if (encapsulation.getClqk().equals("WCL"))
                        encapsulation.setClqk("未处理");
                    else
                        encapsulation.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(encapsulation.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(encapsulation.getScpc());
                    if (proplanByid != null)
                        encapsulation.setScpc(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(encapsulation.getFzry())) {
                    String referdateNameByid = manageService.getMemberStrByid(encapsulation.getFzry());
                    encapsulation.setFzry(referdateNameByid);
                }

                if (!Tools.isNullOrSpace(encapsulation.getProid())) {
                    String cargocateNamwByid = manageService.getCargocateNamwByid(encapsulation.getProid());
                    encapsulation.setProid(cargocateNamwByid);
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", encapsulations);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("saveEncapsulation")
    @ResponseBody
    public HashMap saveEncapsulation(@RequestBody Encapsulation encapsulation) {
        HashMap hashMap = new HashMap();
        encapsulation.setClqk("YCL");

        Proplan proplanByid = manageService.getProplanByid(encapsulation.getScpc());
//        proplanByid.setFzdh(encapsulation.getEncapsulationid());
        int round = (int) Math.round(encapsulation.getCpsl());
        proplanByid.setSjscsl(round);
        manageService.saveProplan(proplanByid);

        try {
            Prohouseid prohouseid = new Prohouseid();
            prohouseid.setProhouseid(Tools.newUUID());
            prohouseid.setScpc(encapsulation.getScpc());
            prohouseid.setClqk("WCL");
            prohouseid.setEncapsulationid(encapsulation.getEncapsulationid());
            prohouseid.setProname(encapsulation.getProid());
            prohouseid.setPronum(encapsulation.getCpsl());

//            prohouseid.setRksj(new Date(System.currentTimeMillis()));
            Prohouseid prohouseidByScpc = manageService.getProhouseidByScpc(encapsulation.getScpc());
            if (prohouseidByScpc == null)
                manageService.saveProhouseid(prohouseid);


            encapsulation.setFzry(manageService.getCurrentUserId());
            manageService.saveEncapsulation(encapsulation);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getEncapsulationByid")
    @ResponseBody
    public HashMap getEncapsulationByid(@RequestBody Encapsulation encapsulation) {
        HashMap hashMap = new HashMap();
        try {
            Encapsulation encapsulationByid = manageService.getEncapsulationByid(encapsulation.getEncapsulationid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", encapsulationByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteEncapsulationByid")
    @ResponseBody
    public HashMap deleteEncapsulationByid(@RequestBody Encapsulation encapsulation) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteEncapsulationByid(encapsulation.getEncapsulationid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }
    /************************Encapsulation杯测**********************/

    /************************Prohouseid入库**********************/

    @PostMapping("saveProhouseid")
    @ResponseBody
    public HashMap saveProhouseid(@RequestBody Prohouseid prohouseid) {
        HashMap hashMap = new HashMap();
        try {
//            判断入库数量是否已经设置；重新设置的需要减去之前的库存在做加法
            Prohouseid prohouseidByid = manageService.getProhouseidByid(prohouseid.getProhouseid());

            Proplan proplanByid = manageService.getProplanByid(prohouseid.getScpc());
//            proplanByid.setRkdh(prohouseid.getProhouseid());
            manageService.saveProplan(proplanByid);


            Kcsl kcslByproid = manageService.getKcslByproid(prohouseid.getProname());
            if (kcslByproid == null) {
                kcslByproid = new Kcsl();
                kcslByproid.setKcslid(Tools.newUUID());
                kcslByproid.setProid(prohouseid.getProname());
                kcslByproid.setPrototalnum(prohouseid.getPronum());
            } else {
                if(prohouseidByid.getClqk().equals("YCL")){
                    kcslByproid.setPrototalnum(kcslByproid.getPrototalnum() + prohouseid.getPronum() - prohouseidByid.getPronum());
                }else{
                    kcslByproid.setPrototalnum(kcslByproid.getPrototalnum() + prohouseid.getPronum());
                }
            }
            manageService.saveKcsl(kcslByproid);


            prohouseid.setClqk("YCL");
            prohouseid.setCkgly(manageService.getCurrentUserId());
            manageService.saveProhouseid(prohouseid);


            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getProhouseidByClqk")
    @ResponseBody
    public HashMap getProhouseidByClqk(@RequestBody Prohouseid prohouseid) {
        HashMap hashMap = new HashMap();
        try {
            List<Prohouseid> prohouseidsByClqk = manageService.getProhouseidsByClqk(prohouseid.getClqk());
            for (Prohouseid prohouseid1 : prohouseidsByClqk) {
                if (!Tools.isNullOrSpace(prohouseid1.getClqk())) {
                    if (prohouseid1.getClqk().equals("WCL"))
                        prohouseid1.setClqk("未处理");
                    else
                        prohouseid1.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(prohouseid1.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(prohouseid1.getScpc());
                    if (proplanByid != null)
                        prohouseid1.setScpc(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(prohouseid1.getCkgly())) {
                    String referdateNameByid = manageService.getMemberStrByid(prohouseid1.getCkgly());
                    prohouseid1.setCkgly(referdateNameByid);
                }

                if (!Tools.isNullOrSpace(prohouseid1.getProname())) {
                    String cargocateNamwByid = manageService.getCargocateNamwByid(prohouseid1.getProname());
                    prohouseid1.setProname(cargocateNamwByid);
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", prohouseidsByClqk);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getProhouseidByid")
    @ResponseBody
    public HashMap getProhouseidByid(@RequestBody Prohouseid prohouseid) {
        HashMap hashMap = new HashMap();
        try {
            Prohouseid prohouseidByid = manageService.getProhouseidByid(prohouseid.getProhouseid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", prohouseidByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }

        return hashMap;
    }

    @PostMapping("deleteProhouseidByid")
    @ResponseBody
    public HashMap deleteProhouseidByid(@RequestBody Prohouseid prohouseid) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteProhouseidByid(prohouseid.getProhouseid());
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getProhouseids")
    @ResponseBody
    public HashMap getProhouseids() {
        HashMap hashMap = new HashMap();
        try {
            List<Prohouseid> prohouseidsByClqk = manageService.getProhouseids();
            for (Prohouseid prohouseid1 : prohouseidsByClqk) {
                if (!Tools.isNullOrSpace(prohouseid1.getClqk())) {
                    if (prohouseid1.getClqk().equals("WCL"))
                        prohouseid1.setClqk("未处理");
                    else
                        prohouseid1.setClqk("已处理");
                }
                if (!Tools.isNullOrSpace(prohouseid1.getScpc())) {
                    Proplan proplanByid = manageService.getProplanByid(prohouseid1.getScpc());
                    if (proplanByid != null)
                        prohouseid1.setScpc(proplanByid.getScpch());
                }
                if (!Tools.isNullOrSpace(prohouseid1.getCkgly())) {
                    String referdateNameByid = manageService.getMemberStrByid(prohouseid1.getCkgly());
                    prohouseid1.setCkgly(referdateNameByid);
                }

                if (!Tools.isNullOrSpace(prohouseid1.getProname())) {
                    String cargocateNamwByid = manageService.getCargocateNamwByid(prohouseid1.getProname());
                    prohouseid1.setProname(cargocateNamwByid);
                }
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", prohouseidsByClqk);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    /************************Prohouseid入库**********************/

    /************************Prodoutbound出库**********************/
    @PostMapping("saveProdoutbound")
    @ResponseBody
    public HashMap saveProdoutbound(@RequestBody Prodoutbound prodoutbound) {
        HashMap hashMap = new HashMap();
        try {
            if (prodoutbound.getProdoutboundid() == null) {
                prodoutbound.setProdoutboundid(Tools.newUUID());
            }

            Kcsl kcslByproid = manageService.getKcslByproid(prodoutbound.getProid());
            Prodoutbound prodoutboundByid = manageService.getProdoutboundByid(prodoutbound.getProdoutboundid());
            if (prodoutboundByid != null) {
                Integer outnum = prodoutboundByid.getOutnum();
                if (outnum > prodoutbound.getOutnum()) {
                    int num = outnum - prodoutbound.getOutnum();
                    kcslByproid.setPrototalnum(kcslByproid.getPrototalnum() + num);
                } else {

                    int num = prodoutbound.getOutnum() - outnum;
                    kcslByproid.setPrototalnum(kcslByproid.getPrototalnum() - num);
                }
            } else {

                kcslByproid.setPrototalnum(kcslByproid.getPrototalnum() - prodoutbound.getOutnum());
            }


            manageService.saveKcsl(kcslByproid);


            manageService.saveProdoutbound(prodoutbound);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getProdoutboundByid")
    @ResponseBody
    public HashMap getProdoutboundByid(@RequestBody Prodoutbound prodoutbound) {
        HashMap hashMap = new HashMap();
        try {
            Prodoutbound prodoutboundByid = manageService.getProdoutboundByid(prodoutbound.getProdoutboundid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", prodoutboundByid);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteProdoutboundByid")
    @ResponseBody
    public HashMap deleteProdoutboundByid(@RequestBody Prodoutbound prodoutbound) {
        HashMap hashMap = new HashMap();
        try {
            Prodoutbound prodoutboundByid = manageService.getProdoutboundByid(prodoutbound.getProdoutboundid());

            Kcsl kcslByproid = manageService.getKcslByproid(prodoutboundByid.getProid());
            kcslByproid.setPrototalnum(kcslByproid.getPrototalnum() + prodoutboundByid.getOutnum());
            manageService.saveKcsl(kcslByproid);

            manageService.deleteProdoutboundByid(prodoutbound.getProdoutboundid());

            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getProdoutbounds")
    @ResponseBody
    public HashMap getProdoutbounds() {
        HashMap hashMap = new HashMap();
        try {
            List<Prodoutbound> prodoutbounds = manageService.getProdoutbounds();
            for (Prodoutbound prodoutbound : prodoutbounds) {
                if (!Tools.isNullOrSpace(prodoutbound.getProid())) {
                    String cargocateNamwByid = manageService.getCargocateNamwByid(prodoutbound.getProid());
                    prodoutbound.setProid(cargocateNamwByid);
                }

            }
            hashMap.put("msg", "ok");
            hashMap.put("list", prodoutbounds);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    /************************Prodoutbound出库**********************/

    /************************Kcsl产品库存表**********************/
    @PostMapping("getKcslByProid")
    @ResponseBody
    public HashMap getKcslByProid(@RequestBody Kcsl kcsl) {
        HashMap hashMap = new HashMap();
        try {
            Kcsl kcslByproid = manageService.getKcslByproid(kcsl.getProid());
            if (kcslByproid != null) {
                int kcslnumByproid = manageService.getKcslnumByproid(kcsl.getProid());
                hashMap.put("msg", "ok");
                hashMap.put("obj", kcslnumByproid);
            } else {
                hashMap.put("msg", "null");
            }
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @GetMapping("getKcsls")
    @ResponseBody
    public HashMap getKcsls() {
        HashMap hashMap = new HashMap();
        try {
            List<Kcsl> kcsls = manageService.getKcsls();
            for (Kcsl kcsl : kcsls) {
                String cargocateNamwByid = manageService.getCargocateNamwByid(kcsl.getProid());
                kcsl.setProid(cargocateNamwByid);
            }
        hashMap.put("msg","ok");
        hashMap.put("list",kcsls);
        } catch (Exception e) {
            hashMap.put("msg","error");
            e.printStackTrace();
        }
        return hashMap;
    }
    /************************Kcsl产品库存表**********************/

    @GetMapping("getRawBeanNum")
    @ResponseBody
    public HashMap getRawBeanNum() throws Exception {


        HashMap m = new HashMap();

//        Object[] result=manageService.getRawBeanNum();
//        m.put("result",result);

        List<Rawbeaninventory> rl = manageService.getRawbeaninventoryList();
        if (rl.isEmpty()) {
            m.put("msg", "empty");
        } else {
            for (Rawbeaninventory r : rl) {
                if (!Tools.isNullOrSpace(r.getPm())) {
                    String pm = manageService.getCargocateNamwByid(r.getPm());
                    r.setPm(pm);
                }

            }
            m.put("msg", "ok");
            m.put("list", rl);


        }


        return m;
    }

    /**********************RawBeanInventoryChange********************************/
    @GetMapping("getRawBeanInventoryChangeList")
    @ResponseBody
    public HashMap getRawBeanInventoryChangeList() throws Exception {

        HashMap m = new HashMap();

        try {
            List<Rawbeaninventorychange> cl = manageService.getRawBeanInventoryChangeList();

            if (cl.isEmpty()) {
                m.put("msg", "empty");

            } else {
                for (Rawbeaninventorychange c : cl) {
                    if (!Tools.isNullOrSpace(c.getBusid())) {
                        String pm = manageService.getCargocateNamwByid(c.getBusid());
                        c.setBusid(pm);
                    }
                }


                m.put("msg", "ok");
                m.put("list", cl);
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }
        return m;
    }


    /**********************RawBeanInventoryChange********************************/


    @GetMapping("getMateriaStaticNum")
    @ResponseBody
    public HashMap getMateriaStaticNum() throws Exception {


        HashMap m = new HashMap();

        List<Materiainventory> ml = manageService.getMateriainventoryList();
        if (ml.isEmpty()) {
            m.put("msg", "empty");
        } else {
            for (Materiainventory x : ml) {
                if (!Tools.isNullOrSpace(x.getPm())) {
                    String pmstr = manageService.getCargocateNamwByid(x.getPm());
                    x.setPm(pmstr);
                }
            }

            m.put("msg", "ok");
            m.put("list", ml);
        }


        return m;
    }

    /************************MateriaInventory****************************/
    @GetMapping("getMateriaInventoryChangeList")
    @ResponseBody
    public HashMap getMateriaInventoryChangeList() throws Exception {
        HashMap m = new HashMap();

        List<Materiainventorychange> ml = manageService.getMateriainventorychangeList();
        if (ml.isEmpty()) {
            m.put("msg", "empty");
        } else {
            for (Materiainventorychange x : ml) {
                if (!Tools.isNullOrSpace(x.getBusid())) {
                    String pm = manageService.getCargocateNamwByid(x.getBusid());
                    x.setBusid(pm);
                }
            }
            m.put("msg", "ok");
            m.put("list", ml);
        }


        return m;
    }

    /************************MateriaInventory****************************/


    @GetMapping("getTicket")
    @ResponseBody
    public HashMap getTicket() {
        HashMap hashMap = new HashMap();
        try {
            String currentUserId = manageService.getCurrentUserId();
            String getticket = authAdminService.getticket(currentUserId);
            hashMap.put("msg", "ok");
            hashMap.put("obj", getticket);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }


        return hashMap;
    }


    @PostMapping("generateBcode")
    @ResponseBody
    public HashMap generateBcode(@RequestBody Encapsulation encapsulation) {
        HashMap hashMap = new HashMap();
        Encapsulation encapsulationByid = manageService.getEncapsulationByid(encapsulation.getEncapsulationid());
        try {
            String scpcid = encapsulationByid.getScpc();
            XWPFDocument doc = new XWPFDocument();// 创建Word文件
            XWPFParagraph p = doc.createParagraph();// 新建段落
            p.setAlignment(ParagraphAlignment.BOTH);// 设置段落的对齐方式
            XWPFRun r = p.createRun();//创建标题
            r.setText("溯源码:" + scpcid);
            r.setBold(true);//设置为粗体
            r.setColor("000000");//设置颜色

            r.setFontSize(15); //设置字体大小
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
            String format = simpleDateFormat.format(new Date(System.currentTimeMillis()));
            File outputfile = new File(filepath + format + "溯源码.docx");
            OutputStream out = new FileOutputStream(outputfile);
            doc.write(out);
            hashMap.put("filepath", outputfile.getName());
            hashMap.put("msg", "ok");
        } catch (IOException e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }

        return hashMap;
    }


/*

    @PostMapping("generateBcode")
    @ResponseBody
    public HashMap generateBcode(@RequestBody Encapsulation encapsulation) {

        HashMap hashMap = new HashMap();
        BlockChain blockChain = new BlockChain();
        try {
            Encapsulation encapsulationByid = manageService.getEncapsulationByid(encapsulation.getEncapsulationid());
            String scpcid = encapsulationByid.getScpc();

            blockChain.setScpcid(scpcid);

            //生产计划表
            Proplan proplanByid = manageService.getProplanByid(scpcid);
            if (proplanByid != null) {
                if (Tools.isNullOrSpace(proplanByid.getScmc())) {
                    blockChain.setScpcmc("");
                } else {
                    blockChain.setScpcmc(proplanByid.getScmc());
                }
            }

            //生豆入库管理表
            Dbd dbdByScpcAndCoffee = manageService.getDbdByScpcAndCoffee(scpcid);
            Coffeeincoming coffeeincomingByid = manageService.getCoffeeincomingByid(dbdByScpcAndCoffee.getJhpc());

            String currentDate = Tools.getCurrentDate();
            Long preMonth = Tools.getPreMonth(currentDate);
            blockChain.setJhsj(coffeeincomingByid.getJhrq().getTime());
            blockChain.setYcljhpc(coffeeincomingByid.getIncompc());
            String referdateNameByid = manageService.getGysNameByid(coffeeincomingByid.getGys());
            blockChain.setYcljhcj(referdateNameByid);

            //烘焙生产表        BlockChain blockChain = new BlockChain();

            Bakerecord bakerecordByScpc = manageService.getBakerecordByScpc(scpcid);
            blockChain.setRdwd(String.valueOf(bakerecordByScpc.getRdwd()));
            blockChain.setHwwd(String.valueOf(bakerecordByScpc.getHwwd()));
            blockChain.setYbwd(String.valueOf(bakerecordByScpc.getYbwd()));
            blockChain.setYbsj(String.valueOf(bakerecordByScpc.getYbsj()));
            blockChain.setCdwd(String.valueOf(bakerecordByScpc.getCdwd()));
            blockChain.setFm(String.valueOf(bakerecordByScpc.getFm()));
            blockChain.setHl(String.valueOf(bakerecordByScpc.getHl()));
            blockChain.setHj(bakerecordByScpc.getHj());
            blockChain.setHbr(manageService.getMemberStrByid(bakerecordByScpc.getHbr()));
            blockChain.setHbrq(bakerecordByScpc.getHbrq().getTime());

            //检验表
            Baketest baketestByScpc = manageService.getBaketestByScpc(scpcid);
            blockChain.setQyrqsj(baketestByScpc.getQysj().getTime());
            blockChain.setSz(String.valueOf(baketestByScpc.getSz()));
            blockChain.setSf(String.valueOf(baketestByScpc.getSf()));
            blockChain.setJcr(manageService.getMemberStrByid(baketestByScpc.getJcr()));

            //杯测表
            Cuptest cuptestByScpc = manageService.getCuptestByScpc(scpcid);
            blockChain.setHbd(String.valueOf(cuptestByScpc.getHbd()));
            blockChain.setBsj(cuptestByScpc.getBcsj().getTime());
            blockChain.setBcry(manageService.getMemberStrByid(cuptestByScpc.getBcry()));
            blockChain.setGx(String.valueOf(cuptestByScpc.getGx()));
            blockChain.setSx(String.valueOf(cuptestByScpc.getSx()));
            blockChain.setFw(String.valueOf(cuptestByScpc.getFw()));
            blockChain.setHg(String.valueOf(cuptestByScpc.getHg()));
            blockChain.setSd(String.valueOf(cuptestByScpc.getSd()));
            blockChain.setChd(String.valueOf(cuptestByScpc.getChd()));
            blockChain.setYzx(String.valueOf(cuptestByScpc.getYzx()));
            blockChain.setPhg(String.valueOf(cuptestByScpc.getPhg()));
            blockChain.setGjd(String.valueOf(cuptestByScpc.getGjd()));
            blockChain.setTd(String.valueOf(cuptestByScpc.getTd()));
            blockChain.setZtfz(String.valueOf(cuptestByScpc.getTotal()));
            blockChain.setBcpj(manageService.getReferdateNameByid(cuptestByScpc.getBcdj()));


            Upb upbloackchain = new Upb();
            upbloackchain.setKey(scpcid);
            upbloackchain.setSj(new Date(System.currentTimeMillis()));
            upbloackchain.setUpblockchainid(Tools.newUUID());

//            manageService.saveUpbloackchain(upbloackchain);
            String key = scpcid;
            String jsonStr = "";
            jsonStr = new Gson().toJson(blockChain);

            boolean flag = blockAdminService.invokeByJson(key, jsonStr);
            if (flag) {
//                String url = "https://mobile.4media.video/home?code=" + key;
                String url = "https://mobile.4media.video/home?code=" + key;
                BufferedImage image = manageService.createImage(url);
                if (image != null) {
                    File outputfile = new File(filepath + Tools.newUUID() + ".jpg");
                    ImageIO.write(image, "jpg", outputfile);
                    hashMap.put("filepath", outputfile.getName());
                }



                Blockchaindb blockchaindb = manageService.getBlockchaindbByKeyname(scpcid);
                if (blockchaindb == null) {
                    blockchaindb = new Blockchaindb();
                    blockchaindb.setKeyname(scpcid);
                    blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                } else {
                    blockchaindb.setSlsj(new Timestamp(System.currentTimeMillis()));
                }
                manageService.saveBlockchaindb(blockchaindb);


                hashMap.put("msg", "ok");
            }
//            hashMap.put("flag", flag);
            hashMap.put("obj", blockChain);
//            hashMap.put("test", jsonStr);


        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }
*/


    @PostMapping("downLoadFormFile")
    @ResponseBody
    public ResponseEntity<ByteArrayResource> downLoadFormFile(@RequestBody PageParm pageParm) throws Exception {

        File file = new File(filepath + pageParm.getParm());
        if (file.exists()) {
            byte[] downloadfile = FileCopyUtils.copyToByteArray(file);
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType("image/jpeg"))
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName())
                    .body(new ByteArrayResource(downloadfile));

        }

        return null;

    }


    @GetMapping("getBlockChainDbs")
    @ResponseBody
    public HashMap getBlockChainDbs() {
        HashMap hashMap = new HashMap();
        try {
            List<Blockchaindb> blockchaindbs = manageService.getBlockchaindbs();
            hashMap.put("list", blockchaindbs);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }


    @PostMapping("getNzCoffeeincomingsByPm")
    @ResponseBody
    public HashMap getJhpclsByPm(@RequestBody Coffeeincoming coffeeincoming) {
        HashMap hashMap = new HashMap();
        List<NzSelOption> result = new ArrayList<>();
        try {
            List<Coffeeincoming> coffeeincomingsByPm = manageService.getCoffeeincomingsByPm(coffeeincoming.getPm());
            for (Coffeeincoming coffeeincoming1 : coffeeincomingsByPm) {
                NzSelOption nzSelOption = new NzSelOption();
                nzSelOption.setValue(coffeeincoming1.getIncomingid());
                nzSelOption.setLabel(coffeeincoming1.getIncompc());
                result.add(nzSelOption);
            }


            hashMap.put("msg", "ok");
            hashMap.put("list", result);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }

        return hashMap;
    }

    @PostMapping("getNzMatincomingsByPm")
    @ResponseBody
    public HashMap getNzMatincomingsByPm(@RequestBody Matincoming matincoming) {
        HashMap hashMap = new HashMap();
        List<NzSelOption> result = new ArrayList<>();
        try {
            List<Matincoming> matincomingsByPm = manageService.getMatincomingsByPm(matincoming.getPm());
            for (Matincoming matincoming1 : matincomingsByPm) {
                NzSelOption nzSelOption = new NzSelOption();
                nzSelOption.setValue(matincoming1.getMatariaincomingid());
                nzSelOption.setLabel(matincoming1.getPc());
                result.add(nzSelOption);
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", result);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }

        return hashMap;
    }

    @GetMapping("getLoginlogs")
    @ResponseBody
    public HashMap getLoginlogs() {
        HashMap hashMap = new HashMap();
        try {
            List<Loginlog> loginlogs = manageService.getLoginlogs();
            hashMap.put("list", loginlogs);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }


        return hashMap;
    }


    @GetMapping("getSearchlogs")
    @ResponseBody
    public HashMap getSearchlogs() {
        HashMap hashMap = new HashMap();
        try {
            List<Searchlog> searchlogs = manageService.getSearchlogs();
            for (Searchlog searchlog : searchlogs) {
                if (!Tools.isNullOrSpace(searchlog.getScpcid())) {
                    Proplan proplanByid = manageService.getProplanByid(searchlog.getScpcid());
                    searchlog.setScpcid(proplanByid.getScpch());
                }
            }
            hashMap.put("list", searchlogs);
            hashMap.put("msg", "ok");
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }

        return hashMap;
    }

    /***************Rawbeaninventory**************/
    @PostMapping("getCoffeeNumByPm")
    @ResponseBody
    public HashMap getCoffeeNumByPm(@RequestBody Rawbeaninventory rawbeaninventory) {
        HashMap hashMap = new HashMap();
        try {
            Rawbeaninventory rawbeaninventoryByPmId = manageService.getRawbeaninventoryByPmId(rawbeaninventory.getPm());
            hashMap.put("msg", "ok");
            hashMap.put("obj", rawbeaninventoryByPmId);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }

        return hashMap;
    }
    /***************Rawbeaninventory**************/
    /***************Materiainventory**************/

    @PostMapping("getMateriainventoryByPm")
    @ResponseBody
    public HashMap getMateriainventoryByPm(@RequestBody Materiainventory materiainventory) {
        HashMap hashMap = new HashMap();
        try {
            Materiainventory materiainventoryByPm = manageService.getMateriainventoryByPm(materiainventory.getPm());
            hashMap.put("msg", "ok");
            hashMap.put("obj", materiainventoryByPm);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }

        return hashMap;
    }

    /***************Materiainventory**************/

}
